package com.submit.demo.util;

import com.submit.demo.common.api.CommonPage;
import lombok.Data;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.regex.Pattern;


@Data
@Component
public class MongoUtil<T> {
    public Integer pageSize;

    private Integer currentPage;

    public void start(Integer currentPage, Integer pageSize, Query query){
        pageSize = pageSize == 0 ? 10 : pageSize;
        query.limit(pageSize);
        query.skip((currentPage - 1) * pageSize);
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    public CommonPage pageHelper(long total, List<T> list){
        return new CommonPage(this.currentPage, total, this.pageSize, list);
    }

    public CommonPage pageHelper(List<T> list){
        return new CommonPage(this.currentPage, this.pageSize, list);
    }

    public CommonPage pageHelper(Integer currentPage, Long total, Integer pageSize, List<T> list){
        return new CommonPage(currentPage, total, pageSize, list);
    }

    public CommonPage pageHelper(Integer currentPage, Integer pageSize, List<T> list){
        return new CommonPage(currentPage, pageSize, list);
    }

    public Pattern getPattern(String string) {
        Pattern pattern = Pattern.compile("^.*" + string + ".*$", Pattern.CASE_INSENSITIVE);
        return pattern;
    }
}
